What's wrong in this C program..? [closed]

Posted by AGeek on Stack Overflow See other posts from Stack Overflow or by AGeek
Published on 2011-01-09T04:24:38Z Indexed on 2011/01/09 4:53 UTC
Read the original article Hit count: 281

Filed under:
struct bucket
{
   int nStrings;        //No. of Strings in a Bucket.
   char strings[MAXSTRINGS][MAXWORDLENGTH];     // A bucket row can contain maximum 9 strings of max string length 10.
};//buck[TOTBUCKETS];

void lexSorting(char array[][10], int lenArray, int symb)       //symb - symbol, sorting based on character symbols.
{
   int i, j;
   int bucketNo;
   int tBuckNStrings;
   bucket buck[TOTBUCKETS];

   for(i=0; i<lenArray; i++)
   {
      bucketNo = array[i][symb] - 'a';          // Find Bucket No. in which the string is to be placed.
      tBuckNStrings = buck[bucketNo].nStrings;  // temp variable for storing nStrings var in bucket structure.
      strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]);   // Store the string in its bucket.
      buck[bucketNo].nStrings = ++tBuckNStrings;        //Increment the nStrings value of the bucket.
   }

//   lexSorting(array, lenArray, ++symb);

   printf("****** %d ******\n", symb);
   for(i=0; i<TOTBUCKETS; i++)
   {
      printf("%c = ", i+'a');
      for(j=0; j<buck[i].nStrings; j++)
         printf("%s ",buck[i].strings[j]);
      printf("\n");
   }
}

int main()
{
   char array[][10] = {"able","aback","a","abet","acid","yawn","yard","yarn","year","yoke"};
   int lenArray = 10;
   int i;

   printf("Strings: ");
   for(i=0; i<lenArray; i++)
      printf("%s ",array[i]);
   printf("\n");

   lexSorting(array, lenArray, 0);
}

Well here is the complete code, that I am trying. since its been a long time since i have touched upon C programming, so somewhere i am making mistake in structure declaration. The problem goes here:-

1) I have declared a structure above and its object as array(buck[]).

2) Now when I declare this object array along with the structure, it works fine.. I have commented this thing right now.

3) But when I declare this object array inside the function.. because ultimately i have to declare inside function( as i need to build a recursive program, where objects will be created in very recursive call) then the program is throwing segmentation fault.

Expected Output

> [others@centos htdocs]$ ./a.out
> Strings: able aback a abet acid yawn
> yard yarn year yoke
> ****** 0 ****** 
> a = able aback a abet acid 
> b = 
> c 
> .
> .
> y = yawn yard yarnyear yoke 
> z =

Actual Output

[others@centos htdocs]$ ./a.out
Strings: able aback a abet acid yawn yard yarn year yoke
Segmentation fault

I have no idea, what difference I made in this. Kindly help.

Thanks.

© Stack Overflow or respective owner

Related posts about c